<?php

require_once dirname(__FILE__)."/../db/db.php";
require_once 'stockData.php';

define("TBLSTOCKDATA_FLD_TS", "timestamp");
define("TBLSTOCKDATA_FLD_TICK", "tick");
define("TBLSTOCKDATA_FLD_CLOSE", "close");
define("TBLSTOCKDATA_FLD_VOLUME", "volume");
define("TBLSTOCKDATA_FLD_OPEN", "open");
define("TBLSTOCKDATA_FLD_LOW", "low");
define("TBLSTOCKDATA_FLD_HIGH", "high");


class tblStockData extends dbTable{
	protected $companyName;
	
	public function __construct($coName){
		$this->companyName = $coName;
		
		$tableName = "stockData_".$this->companyName."_tick";
		$keyValue = array(TBLSTOCKDATA_FLD_TS, TBLSTOCKDATA_FLD_TICK);
		
		parent::__construct($tableName, $keyValue);
	}
	
	public function CreateTable(){
		$sql = "CREATE TABLE $this->name (".TBLSTOCKDATA_FLD_TS." DATETIME NOT NULL, ";
		$sql .= TBLSTOCKDATA_FLD_TICK." INT NOT NULL AUTO_INCREMENT, ";
		$sql .= TBLSTOCKDATA_FLD_CLOSE." FLOAT NOT NULL, ";
		$sql .= TBLSTOCKDATA_FLD_VOLUME." FLOAT NOT NULL, ";
		$sql .= "PRIMARY KEY(".TBLSTOCKDATA_FLD_TS.", ".TBLSTOCKDATA_FLD_TICK."))";
		
		//importante!!! altrimenti non permette l'uso della primary key (timestamp, tick) con tick che e' autoincrement
		$sql .= " ENGINE = MyISAM;";
		
		$this->db->query($sql);
	}
	
	protected function record2object($assocArrayRec){
		return new stockData($assocArrayRec[TBLSTOCKDATA_FLD_TS], $assocArrayRec[TBLSTOCKDATA_FLD_CLOSE], $assocArrayRec[TBLSTOCKDATA_FLD_VOLUME]);
	}
	
	protected function object2record($obj){
		$assocArrayRecord = array();
		$assocArrayRecord[TBLSTOCKDATA_FLD_TS] = date("Y-m-d H:i:s", strtotime($obj->dateTime));
		$assocArrayRecord[TBLSTOCKDATA_FLD_CLOSE] = $obj->close;
		$assocArrayRecord[TBLSTOCKDATA_FLD_VOLUME] = $obj->volume;
		
		return $assocArrayRecord;
	}

	public function Insert($stockDataItem){
		$recordStock = $this->object2record($stockDataItem);
		parent::Insert($recordStock);
	}
}

class TestStockData{
	private $tblTest;
	
	public function __construct(){
		$this->tblTest = new tblStockData("AAPL");
	}

	public function Test(){
		$exampleStockData1 = new stockData("02/02/2015 09:00:00", 15.15, 1230);
		$this->tblTest->Insert($exampleStockData1);

		$exampleStockData2 = new stockData("02/02/2015 09:00:00", 15.14, 2000);
		$this->tblTest->Insert($exampleStockData2);
		
		$exampleStockData3 = new stockData("02/02/2015 09:00:01", 15.12, 3500);
		$this->tblTest->Insert($exampleStockData3);
		
		$this->tblTest->Show();
	}
}

class tblStockDataEx extends dbTable{
	protected $companyName;
	
	public function __construct($coName){
		$this->companyName = $coName;

		$tableName = "stockData_".$this->companyName;
		$keyValue = array(TBLSTOCKDATA_FLD_TS);
		
		parent::__construct($tableName, $keyValue);
	}
	
	public function CreateTable(){
		$sql = "CREATE TABLE $this->name (".TBLSTOCKDATA_FLD_TS." DATETIME NOT NULL, ";
		$sql .= TBLSTOCKDATA_FLD_OPEN." FLOAT NOT NULL, ";
		$sql .= TBLSTOCKDATA_FLD_CLOSE." FLOAT NOT NULL, ";
		$sql .= TBLSTOCKDATA_FLD_HIGH." FLOAT NOT NULL, ";
		$sql .= TBLSTOCKDATA_FLD_LOW." FLOAT NOT NULL, ";
		$sql .= TBLSTOCKDATA_FLD_VOLUME." FLOAT NOT NULL, ";
		$sql .= "PRIMARY KEY(".TBLSTOCKDATA_FLD_TS."))";

		$this->db->query($sql);
	}
	
	protected function record2object($assocArrayRec){
		return new stockDataEx(	$assocArrayRec[TBLSTOCKDATA_FLD_TS], 
								$assocArrayRec[TBLSTOCKDATA_FLD_OPEN],
								$assocArrayRec[TBLSTOCKDATA_FLD_CLOSE],
								$assocArrayRec[TBLSTOCKDATA_FLD_LOW],
								$assocArrayRec[TBLSTOCKDATA_FLD_HIGH],
								$assocArrayRec[TBLSTOCKDATA_FLD_VOLUME]);
	}
	
	protected function object2record($obj){
		$assocArrayRecord = array();
		$assocArrayRecord[TBLSTOCKDATA_FLD_TS] = date("Y-m-d H:i:s", strtotime($obj->dateTime));
		$assocArrayRecord[TBLSTOCKDATA_FLD_OPEN] = $obj->open;
		$assocArrayRecord[TBLSTOCKDATA_FLD_CLOSE] = $obj->close;
		$assocArrayRecord[TBLSTOCKDATA_FLD_LOW] = $obj->low;
		$assocArrayRecord[TBLSTOCKDATA_FLD_HIGH] = $obj->high;
		$assocArrayRecord[TBLSTOCKDATA_FLD_VOLUME] = $obj->volume;
		
		return $assocArrayRecord;
	}

	public function Insert($stockDataItem){
		$recordStock = $this->object2record($stockDataItem);
		parent::Insert($recordStock);
	}
}

class TestStockDataEx{
	private $testTbl;
	
	public function __construct(){
		$this->testTbl = new tblStockDataEx("ENI");
	}
	
	public function Test(){
		$example1 = new stockDataEx("03/02/2015 15:30:00", 13.00, 14.33, 12.11, 15.4, 1200);
		$this->testTbl->Insert($example1);
		
		$example2 = new stockDataEx("03/02/2015 15:30:01", 14.00, 14.00, 14.00, 14.09, 200);
		$this->testTbl->Insert($example2);

		$example3 = new stockDataEx("03/02/2015 15:30:02", 13.00, 14.33, 12.11, 15.34, 2500);
		$this->testTbl->Insert($example3);
		
		$this->testTbl->Show();
	}
}

?>
